Ext.define('CS.panel.Multi', {
	extend: 'CS.panel.HBox',
	requires: ['CS.panel.VBox'],

	headCls: Ext.baseCSSPrefix + 'multi-head',

	infoPanel: undefined,

	headPanel: undefined,

	contentPanel: undefined,

	operatePanel: undefined,

	deletePanel: undefined,

	constructor: function(config) {
		var me = this;

		config = CS.apply({}, config);

		config.tools = [{
			type: 'plus',
			tooltip: '新增',
			handler: function() {
				me.addRow();
			}
		}, {
			type: 'minus',
			tooltip: '清空',
			handler: function() {
				CS.confirm('将会清空所有数据，请确认', function() {
					me.clear();
				});
			}
		}];

		me.createHead(config);
		me.createContent(config);
		me.createInfo(config);
		me.createDelete(config);
		me.createOperate(config);

		config.items = [config.infoPanel, config.operatePanel];

		me.callParent([config]);
	},

	initComponent: function() {
		var me = this;

		me.addEvents('beforeaddrow', 'addrow', 'beforeremoverow', 'removerow');

		me.callParent(arguments);
	},

	createHead: function(config) {
		var me = this;

		var headConfig = {
			xtype: 'panel.hbox',
			border: false,
			defaults: {
				xtype: 'panel',
				bodyCls: me.headCls,
				margin: '3 0 3 3'
			},
			items: []
		};

		Ext.each(config.items, function(item) {
			headConfig.items.push({
				html: item.fieldLabel,
				colWidth: item.colWidth
			});
		});

		config.headPanel = CS.create(headConfig);

		return config.headPanel;
	},

	createContent: function(config) {
		var me = this, itemConfig = config.items;

		Ext.each(itemConfig, function(item) {
			item.itemId = item.clsParam;
			delete item.fieldLabel;
		});

		config.contentPanel = CS.create({
			xtype: 'panel.vbox',
			border: false,
			defaults: {
				xtype: 'panel.hbox',
				border: false,
				defaults: {
					margin: '0 0 3 3'
				}
			},
			defaultConfig: {
				xtype: 'panel.hbox',
				plugins: ['paneldatadeal', 'paneldomdeal'],
				border: false,
				defaults: {
					margin: '0 0 3 3'
				},
				items: itemConfig
			},
			addRow: function(cmp) {
				if (!cmp) {
					cmp = me.createRow(me.contentPanel.defaultConfig);
				}

				return me.contentPanel.add(cmp);
			}
		});

		return config.contentPanel;
	},

	createRow: function(config) {
		config = CS.apply({}, config);

		return CS.create(config);
	},

	createInfo: function(config) {
		config.infoPanel = CS.create({
			xtype: 'panel.vbox',
			border: false,
			items: [config.headPanel, config.contentPanel]
		});

		return config.infoPanel;
	},

	createDelete: function(config) {
		var me = this;

		config.deletePanel = CS.create({
			xtype: 'panel.vbox',
			border: false,
			addDelete: function(row) {
				var button = CS.create({
					xtype: 'button',
					text: '删除',
					margin: '0 3 3 3',
					handler: function() {
						me.removeRow(me.deletePanel.items.indexOf(button));
					}
				});

				me.deletePanel.add(button);

				return button;
			}
		});

		return config.deletePanel;
	},

	createOperate: function(config) {
		var me = this;

		config.operatePanel = CS.create({
			xtype: 'panel.vbox',
			colWidth: 100,
			border: false,
			items: [{
				xtype: 'panel',
				border: true,
				bodyCls: me.headCls,
				margin: '3',
				html: '操作'
			}, config.deletePanel]
		});

		return config.operatePanel;
	},

	addRow: function(cmp) {
		var me = this;

		me.fireEvent('beforeaddrow', cmp);

		var row = me.contentPanel.addRow(cmp);
		var button = me.deletePanel.addDelete(row);

		me.fireEvent('addrow', row, button);

		return row;
	},

	count: function() {
		return this.contentPanel.items.getCount();
	},

	getRow: function(num) {
		if (CS.isNull(num)) {
			num = me.count() - 1;
		}

		if (num >= 0) {
			return this.contentPanel.getComponent(num).items.getRange();
		} else {
			return null;
		}
	},

	getRows: function(start, end) {
		var me = this, itemArr = [];

		if (CS.isNull(start)) {
			start = 0;
		}

		if (CS.isNull(end)) {
			end = me.count() - 1;
		}

		for (; start <= end; start++) {
			var row = me.contentPanel.getComponent(start);
			itemArr.push(row.items.getRange());
		}

		return itemArr;
	},

	removeRow: function(num) {
		var me = this;

		if (CS.isNull(num)) {
			num = me.count() - 1;
		}

		if (num >= 0) {
			var row = me.contentPanel.getComponent(num);
			var button = me.deletePanel.getComponent(num);

			me.fireEvent('beforeremoverow', num, row, button);

			me.contentPanel.remove(row);
			me.deletePanel.remove(button);

			me.fireEvent('removerow', num);
		}
	},

	reload: function(params, callbackFn, lock) {
		var me = this;

		CS.ajax({
			url: me.url,
			data: params,
			lock: lock,
			succ: function(data) {
				me.setData(data.data, true);
			}
		});
	},

	getData: function(notNeedCls) {
		var me = this, data = [], clsName = me.clsName, rows = me.getRows();

		Ext.each(rows, function(row) {
			var rowData = {};

			Ext.each(row, function(item) {
				rowData[item.clsParam] = item.getData();
			});

			data.push(rowData);
		});

		return CS.getPackageData(data, clsName, notNeedCls);
	},

	setData: function(data, notNeedCls) {
		var me = this;

		if (notNeedCls != true) {
			data = data[me.clsName];
		}

		if (Ext.isObject(data)) {
			data = [data];
		}

		for (var i = 0; i < data.length; i++) {
			var rowData = data[i];

			me.addRow().items.each(function(item) {
				item.setData(rowData[item.clsParam]);
			});
		}
	},

	clear: function() {
		var size = this.count();

		for (var i = 0; i < size; i++) {
			this.removeRow();
		}
	},

	children: function() {
		var children = [], rows = this.getRows();

		Ext.each(rows, function(row) {
			children = children.concat(row);
		});

		return children;
	},

	getSibling: function(cmp, name) {
		return cmp.up().get(name);
	}

});
